import 'package:beautyflutter/pages/home/card_free.dart';
import 'package:beautyflutter/pages/home/card_recommend.dart';
import 'package:beautyflutter/pages/home/card_share.dart';
import 'package:beautyflutter/pages/home/card_spacial.dart';
import 'package:beautyflutter/pages/home/custom_appbar.dart';
import 'package:beautyflutter/pages/home/person.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class ContentPager extends StatefulWidget {
  final ValueChanged<int> onPageChanged;
  final ContentPagerConteroller contentPagerConteroller;
  //构造方法
  const ContentPager({Key key, this.onPageChanged, this.contentPagerConteroller}) : super(key: key);
  @override
  _ContentPagerState createState() => _ContentPagerState();
}

class _ContentPagerState extends State<ContentPager> {
  PageController _pageController = PageController(
    viewportFraction: 1
  );
  @override
  void initState() {
    if(widget.contentPagerConteroller != null) {
      widget.contentPagerConteroller._pageController = _pageController;
    }
    _statusBar();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        //appBar
        CustomAppBar(),
        Expanded(
          //高度撑开
          child: PageView(
            controller: _pageController,
            onPageChanged: widget.onPageChanged,
            children: <Widget>[
              _warpItem(CardRecommend()),
              _warpItem(CardShare()),
              _warpItem(CardFree()),
              _warpItem(CardSpecial()),
              _warpItem(Person()),
            ],
          )
        ),
      ],
    );
  }
  Widget _warpItem (Widget widget) {
    return Padding(padding: EdgeInsets.all(10),
      child: widget,
    );
  }
  _statusBar() {
    SystemUiOverlayStyle uiOverlayStyle = SystemUiOverlayStyle(
      systemNavigationBarColor: Color(0xFF000000),
      systemNavigationBarDividerColor: null,
      statusBarColor: Colors.transparent,
      systemNavigationBarIconBrightness: Brightness.light,
      statusBarIconBrightness: Brightness.dark,
      statusBarBrightness: Brightness.light,
    );
    SystemChrome.setSystemUIOverlayStyle(uiOverlayStyle);
  }
}
class ContentPagerConteroller {
  PageController _pageController;
  void jumpToPage(int page) {
    //?. 安全的调用
    print(_pageController);
    _pageController?.jumpToPage(page);
  }
}
